package cs757;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

public class DlgAddProfessorToProject extends JDialog {

	private static final long serialVersionUID = -1423295580949289283L;

	private DefaultListModel list;
	
	//Database connection
	private DatabaseConnector conn;
	
	//Key of the project that we are adding this professor to
	private String recordID;
	
	private boolean ok = false;
	
	private final JPanel contentPanel = new JPanel();
	private JComboBox comboBoxProfessor;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try { 
			DlgAddProfessorToProject dialog = new DlgAddProfessorToProject();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DlgAddProfessorToProject() {
		setBounds(100, 100, 385, 242);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel lblAddProfessor = new JLabel("Add professor:");
		
		comboBoxProfessor = new JComboBox();
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(64)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(comboBoxProfessor, GroupLayout.PREFERRED_SIZE, 202, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblAddProfessor))
					.addContainerGap(166, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(40)
					.addComponent(lblAddProfessor)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(comboBoxProfessor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(143, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						addProfessorToList();
						closeDialog();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						closeDialog();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	
	/**
	 * Add the professor to the project's list of professors
	 * Insert a record for the professor into the works_in table in the DB, and update the GUI list
	 */
	private void addProfessorToList(){
	
		try {  
			String professorIDandName = (String)comboBoxProfessor.getSelectedItem();
			String professorID = professorIDandName.substring(0, professorIDandName.indexOf('-'));
			
			Object[] values = new Object[]{
					Integer.parseInt(recordID),
					professorID
				};
			conn.executeInsert(DatabaseConnector.INSERT_GROUP_MEMBER_PROFESSOR, values);
			list.addElement(comboBoxProfessor.getSelectedItem());

		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Error querying database.\nQuery:"+DatabaseConnector.INSERT_GROUP_MEMBER_PROFESSOR,
					"Error In Query", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	
	/**
	 * Query the professors table to list the available professors
	 */
	protected void populateProfessorComboBox(){
		try {
			Vector<Vector<Object>> results = conn.executeQuery(DatabaseConnector.PROFESSOR_WORKS_AVAILABLE, 2);
			
			for(Vector<Object> row:results){
				comboBoxProfessor.addItem(row.get(0)+" - "+row.get(1));
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Error querying major IDs from department table.\n ",
					"Error In Query", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/**
	 * Set the list model for the professors combo box
	 * @param list
	 */
	public void setList(DefaultListModel list){
		this.list = list;
	}
	

	/**
	 * Close the add student GUI
	 */
	private void closeDialog(){
		this.setVisible(false);
	}

	/**
	 * @return the ok
	 */
	public boolean isOk() {
		return ok;
	}

	/**
	 * @param conn the conn to set
	 */
	public void setConn(DatabaseConnector conn) {
		this.conn = conn;
	}
	
	/**
	 * @param recordID the recordID to set
	 */
	public void setRecordID(String recordID) {
		this.recordID = recordID;
	}

}
